package org.example.gateway;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class CustomGatewayFilterFactory extends AbstractGatewayFilterFactory<CustomConfig> implements Ordered {

    public CustomGatewayFilterFactory(Class<CustomConfig> configClass) {
        super(CustomConfig.class);
    }

    @Override
    public GatewayFilter apply(CustomConfig config) {
        return new GatewayFilter() {
            //HTTP请求与响应，提供了对HTTP请求与响应的访问
            //ServerWebExchange，提供了对HTTP请求与响应的访问
            //GatewayFilterChain，提供了对请求与响应的访问
            //Mono，提供了对异步编程的支持
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                //Pre类型  Post类型
                //Pre类型：在请求被路由之前执行
                return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                    //Post类型：在请求被路由之后执行

                }));
            }
        };
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
